﻿using DTO;
using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HDT.GUI
{
    public partial class FrmSuaLoaiMH : Form
    {
        private LoaiMatHangDTO loaiMH;
        public delegate void MyDelegate();
        public MyDelegate refreshForm;

        public FrmSuaLoaiMH()
        {
            InitializeComponent();
        }

        public FrmSuaLoaiMH(string maLMH)
            : this()
        {
            try
            {
                loaiMH = new LoaiMatHangBLL().GetByID(maLMH);
            }
            catch
            {
                loaiMH = new LoaiMatHangDTO();
            }
            LoadLoaiMatHangInfo();
        }

        private void LoadLoaiMatHangInfo()
        {
            txtMaLoaiMH.Text = loaiMH.MaLoaiMatHang;
            txtLoaiMH.Text = loaiMH.TenLoaiMatHang;
            cboTrangThai.SelectedIndex = loaiMH.TrangThai ? 0 : 1;
        }

        private void SetLoaiMatHangInfo()
        {
            loaiMH.TenLoaiMatHang = txtLoaiMH.Text.Trim();
            loaiMH.TrangThai = cboTrangThai.SelectedIndex == 0;
            new LoaiMatHangBLL().Update(loaiMH);
        }

        private void btnQuayLai_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            btnSuaLoaiMH.Enabled = true;
        }

        private void FrmSuaLoaiMH_Load(object sender, EventArgs e)
        {
            btnSuaLoaiMH.Enabled = false;
        }

        private void btnSuaLoaiMH_Click(object sender, EventArgs e)
        {
            try
            {
                SetLoaiMatHangInfo();
                MessageBox.Show("Cập nhật loại mặt hàng thành công", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                refreshForm();
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
